<?php

namespace App\Models\V5;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class CommentReply extends Model
{
    protected $table = 'commentreply';
    const CREATED_AT = 'AddDate';
    const UPDATED_AT = 'ReleaseDate';
    protected $fillable = [
        'CommID',
        'Type',
        'ReaParID',
        'CommContents',
        'reply_MemNumber',
        'pid',
        'reply_MemNumber',
        'MemNumber',
        'DeleteMark'
    ];

    //单条
    public function getPlayComment($CommID, $readPartyId)
    {
        $tmp  = [];
        $data = DB::table('commentreply')->where(['CommID' => $CommID, 'ReaParID' => $readPartyId])
            ->select('commentreply.*', 'm.YourName as userName', 'm.UserID','m.FilePath1 as userAvg', 'mm.YourName as replyName','mm.UserID as replyUserId')
            ->join('memberinfo as m', 'm.MemNumber', '=', 'commentreply.MemNumber')
            ->leftJoin('memberinfo as mm', 'commentreply.reply_MemNumber', '=', 'mm.MemNumber')
            ->orderBy('commentreply.AddDate')
            ->get();
        foreach ($data as $key => &$common) {
            $common->CommContents = emoji_text_decode($common->CommContents);
            $common->userAvg      = change_member_head_img($common->userAvg);
            if ($common->DeleteMark == 0) {
                unset($data[$key]);
            } else {
                $tmp[] = $common;
            }
        }
        return $tmp;
    }
}